﻿using Bricks.Business.Models.Account;
using Bricks.Business.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bricks.Component.Utils;
using Bricks.Component.Utils.Extensions;
using Bricks.Site.Models.Purview;

namespace Bricks.Site.Web.Controllers
{
    /// <summary>
    /// 权限组管理
    /// </summary>
    [Export]
    public class RoleInfoController : BaseController
    {
        [Import]
        public IRoleInfoService RoleInfoService { get; set; }

        /// <summary>
        /// 菜单管理首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            //取得面板及工具栏信息
            //ViewBag.PanelInfo = base.GetPanelInfo();
            ViewBag.ToolBarInfo = base.GetToolBarInfo();
            return View();
        }

        /// <summary>
        /// 获取数据列表/树表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetRoleDataList()
        {
            var queryData = RoleInfoService.Roles.OrderBy(m => m.OrderNo).ToList();  //转换为List之后，可进行函数操作
            var data = from m in queryData
                       select new
                       {
                           iconCls = "icon-application",  //固定字段，作为treeField的图标
                           m.ID,
                           m.RoleName,
                           m.RoleCode,
                           m.IsDeleted,
                           m.Description,
                           m.OrderNo,
                           m.PID
                       };
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取权限组下拉项数据（树形）
        /// </summary>
        /// <returns></returns>
        public ActionResult GetRoleComboBoxList()
        {
            var queryData = RoleInfoService.Roles.OrderBy(m => m.OrderNo);  //转换为List之后，可进行函数操作
            var data = from m in queryData
                       select new
                       {
                           iconCls = "icon-application",  //固定字段，作为treeField的图标
                           id = m.ID,
                           text = m.RoleName,
                           parent = m.PID,
                       };
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 新增或编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(string id, string pid)
        {
            RoleInfo model = new RoleInfo();
            //如果是编辑
            if (!String.IsNullOrEmpty(id))
            {
                Guid guidID = id.ToGuid();
                var record = RoleInfoService.Roles.FirstOrDefault(m => m.ID == guidID);
                if (record != null)
                {
                    model = record;
                    if (!model.PID.HasValue)
                        model.PID = Guid.Empty;
                }
            }
            //如果是新增
            else
            {
                //如果选中了节点，点新增，那么则将其父级设置为选中的节点
                if (!String.IsNullOrEmpty(pid))
                {
                    model.PID = Guid.Parse(pid);
                }
                else
                {
                    model.PID = Guid.Empty;
                }
            }
            return View(model);
        }

        /// <summary>
        /// 新增或编辑（响应）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(RoleInfo model)
        {
            var result = new OperationResult(OperationResultType.NoChanged);
            var record = RoleInfoService.Roles.FirstOrDefault(m => m.ID == model.ID);

            //如果选择了无父级，那应该把PID置空
            if (model.PID.Equals(Guid.Empty))
                model.PID = null;
            //新增
            if (record == null)
            {
                result = RoleInfoService.Add(model);
                //if (result.ResultType == OperationResultType.Success)
                //    return View(result.AppendData);
            }
            //修改
            else
            {
                result = RoleInfoService.Modify(model);
                //if (result.ResultType == OperationResultType.Success)
                //    return View(result.AppendData);
            }
            //return View();
            return Json(result, "text/html");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(string id)
        {
            Guid guidID = id.ToGuid();
            var exeResult = RoleInfoService.Delete(guidID);
            return Json(exeResult);
        }

        /// <summary>
        /// 排序设置
        /// </summary>
        /// <param name="id">ID</param>
        /// <param name="pid">PID</param>
        /// <param name="order">排序</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SetOrder(string id, string pid, string order)
        {
            Guid guidID = id.ToGuid();
            Guid? guidPID = pid.ToGuid();
            if (guidPID.Equals(Guid.Empty))
                guidPID = null;

            var exeResult = RoleInfoService.SetOrder(guidID, guidPID, order);
            return Json(exeResult);
        }

        /// <summary>
        /// 获取下拉树（选择父级使用）
        /// </summary>
        /// <returns></returns>
        public ActionResult GetRoleComboTree()
        {
            var queryData = RoleInfoService.Roles.OrderBy(m => m.OrderNo).ToList();

            //添加无父级节点
            RoleInfo nullParent = new RoleInfo();
            nullParent.ID = Guid.Empty;
            nullParent.RoleName = "无父级";
            nullParent.PID = null;
            queryData.Insert(0, nullParent);  //插入到第一个

            var data = from m in queryData
                       select new
                       {
                           iconCls = "icon-application",  //固定字段，作为treeField的图标
                           id = m.ID,
                           text = m.RoleName,
                           parent = m.PID,
                       };
            return Json(data, JsonRequestBehavior.AllowGet);
        }
    }
}
